# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request
from jobs.items import JobsItem

class Jobs58Spider(scrapy.Spider):
    name = "jobs58"
    allowed_domains = ["58.com"]
    start_urls = ['http://sh.58.com/job/']
    for i in range(2, 71):
        start_urls.append('http://sh.58.com/job/pn%d/' % i)

    def parse(self, response):
        links = response.xpath('//a[@class="t"]/@href').extract()
        for link in links:
            yield Request(link, callback=self.getDetail)

    def getDetail(self, response):
        posinfo = response.xpath('//div[@class="item_con pos_info"]')
        title = posinfo.xpath('.//span[@class="pos_title"]/text()')[0].extract()
        try:
            salary = posinfo.xpath('.//span[@class="pos_salary"]/text()')[0].extract()
        except:
            salary = '0'
        num = posinfo.xpath('.//span[@class="item_condition pad_left_none"]/text()')[0].extract()
        edu = posinfo.xpath('.//span[@class="item_condition"]/text()')[0].extract()
        exp = posinfo.xpath('.//span[@class="item_condition border_right_None"]/text()')[0].extract()
        area = posinfo.xpath('.//span[@class="pos_area_span pos_address"]//a/text()').extract()
        item = JobsItem()
        item['title'] = title.strip()
        item['salary'] = salary.strip()
        item['num'] = num.strip('招人 ')
        item['edu'] = edu.strip()
        item['exp'] = exp.strip()
        item['location'] = area
        yield item